﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Micrhard.FMCG.Server.Interface;
using Microsoft.Practices.Unity;
using Micrhard.FMCG.Server.Models;
using Microhard.Permission.Interface;
using Microhard.Permission.MVCFilter;
using Microhard.WebUI.Models;
using Microhard.Core;
using Telerik.Web.Mvc;
using Microhard.Core.DBUtility;
using Microhard.Permission.MVCFilter.MVCFilter;
using Microhard.Framework.MVCHelper;
using FluentValidation.Mvc;
using Microhard.FMCG.WebUI.Areas.Inspection.Models;

namespace Microhard.FMCG.WebUI.Areas.Inspection.Controllers
{
    public class IspItemController : MicroHardBaseController<IspItemModel>
    {
        private IIspItem _resp = null;
        [Dependency]
        public IIspItem Category
        {
            set
            {
                base._repository = value;
                _resp = value;
            }
            get
            {
                return _resp;
            }

        }

     
        //预览
        public ActionResult Preview()
        {
            return View();
        }

        //设置首页
        [MVCFilterAtrForWeixinMP()]
        [MVCFilterAtrForLogon]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName="Index")]//功能权限验证
        [MVCFilterAtrForDataPermission(OnExecutingFilterDataType = "Index")]//数据权限验证
        public ActionResult IspItemSet(Guid id, ScopeResult scope)
        {
           
            IIspTemplateAnswer temp = IoC.Resolve<IIspTemplateAnswer>();
            temp.scope = scope;
            int totalCount;
            List<IspTemplateAnswerModel> rtn = null;
            ItemGridModel gm = new ItemGridModel();

            IspTemplateAnswerModel model = new IspTemplateAnswerModel();
            model.ItemID = id;
            rtn = temp.Query(model, 1, 10, out totalCount);
                gm.Total = totalCount;
                gm.ItemId = id;
              
            gm.Data = rtn;
            return View(gm);
        }

        //检查项明细分页显示
        [MVCFilterAtrForWeixinMP()]
        [MVCFilterAtrForLogon]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Index")]//功能权限验证
        [MVCFilterAtrForDataPermission(OnExecutingFilterDataType = "Index")]//数据权限验证
        public ActionResult IspItemSetPageList(GridCommand command, Guid ItemID, ScopeResult scope)
        {
            IIspTemplateAnswer temp = IoC.Resolve<IIspTemplateAnswer>();
            int totalCount;
            temp.scope = scope;
            IspTemplateAnswerModel model = new IspTemplateAnswerModel();
            model.ItemID = ItemID;
            var rtn = temp.Query(model, command.Page, command.PageSize, out totalCount);       

            var userModel = new GridModel()
            {
                Data = rtn,
                Total = totalCount
            };
            return new JsonResult
            {
                Data = userModel
            };
        }

        [HttpPost]
        [MVCFilterAtrForLogon()]//登录验证
         [MVCFilterAtrForResourcePermission(OperationKeyName = "Update")]//功能权限验证
        [MVCFilterAtrForDataPermission(OnExecutingFilterDataType = "Update")]//数据权限验证
        public ActionResult IspItemSetUpdate(Guid identity, ScopeResult scope)
        {
            IIspTemplateAnswer temp = IoC.Resolve<IIspTemplateAnswer>();
            temp.scope = scope;
            var model = temp.GetModelByID(identity);
            BasePermissionModel bpm = model as BasePermissionModel;
            if (bpm != null)
            {
                bpm.OperationStatus = OperationStatus.Update;            
            }
            return View(model);    
        }

        [MVCFilterAtrForWeixinMP()]
        [MVCFilterAtrForLogon()]//登录验证
       [MVCFilterAtrForResourcePermission(OperationKeyName = "Create")]//功能权限验证
        [MVCFilterAtrForDataPermission]//数据权限验证
        public ActionResult IspItemSetCreate(Guid ItemID)
        {
           
            var model = new IspTemplateAnswerModel();
            model.ItemID = ItemID;
            BasePermissionModel bpm = model as BasePermissionModel;
            if (bpm != null)
            {
                bpm.OperationStatus = OperationStatus.Add;
            }
            return View(model);
        }

        [HttpPost]
        [MVCFilterAtrForLogon()]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Delete")]//功能权限验证
        [MVCFilterAtrForDataPermission]//数据权限验证
        public ActionResult IspItemSetDelete(List<Guid> identities, ScopeResult scope)
        {
            IIspTemplateAnswer temp = IoC.Resolve<IIspTemplateAnswer>();
            temp.scope = scope;
            var result = temp.Delete(identities);
            if (result.IsSucceed)
            {
                this.HttpContext.Items["ToDeleteIds"] = identities;
                return new JsonResult { Data = "succeed" };
            }
            return new JsonResult { Data = result.ErrorMsgTxt };
        }

        [HttpPost]
        [MVCFilterAtrForWeixinMP()]      
        [MVCFilterAtrForLogon()]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "SaveCreate")]//功能权限验证
        [MVCFilterAtrForDataPermission]//数据权限验证
        public  ActionResult IspItemSaveCreate([CustomizeValidator(RuleSet = "Insert")]IspTemplateAnswerModel model, ScopeResult scope)
        {
            string newViewName = null;
            if (this.HttpContext.Items[SysConst.REDIRECTTONEWVIEW] != null)
            {
                newViewName = this.HttpContext.Items[SysConst.REDIRECTTONEWVIEW].ToString();
            }

            IIspTemplateAnswer temp = IoC.Resolve<IIspTemplateAnswer>();
            if (ModelState.IsValid)
            {
                temp.scope = scope;
                var rtn = temp.Add(model);
                rtn.ExecutedModel = model;
                this.HttpContext.Items["executedResult"] = rtn;

                if (!rtn.IsSucceed)
                {
                    StringBuilder errorMsg = new StringBuilder();
                    foreach (var err in rtn.ErrorMsg)
                    {
                        errorMsg.Append(err.Value);
                    }
                    TempData["executedResult"] = errorMsg.ToString();
                    return View((string.IsNullOrEmpty(newViewName) == false ? newViewName : "IspItemSetCreate"), model);
                }
                else
                {
                    return RedirectToAction("IspItemSet", new { id = model.ItemID });
                }

            }

            return RedirectToAction("IspItemSet", new { id = model.ItemID });
        }

        [HttpPost]
        [MVCFilterAtrForLogon()]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "SaveUpdate")]//功能权限验证
        [MVCFilterAtrForDataPermission]//数据权限验证
        public  ActionResult IspItemSaveUpdate([CustomizeValidator(RuleSet = "Update")]IspTemplateAnswerModel model, ScopeResult scope)
        {
            IIspTemplateAnswer temp = IoC.Resolve<IIspTemplateAnswer>();
            if (ModelState.IsValid)
            {
                temp.scope = scope;
                var rtn = temp.Update(model);

                rtn.ExecutedModel = model;
                this.HttpContext.Items["executedResult"] = rtn;

                if (rtn.IsSucceed)
                {
                    return RedirectToAction("IspItemSet", new { id = model.ItemID });
                }
                else
                {
                    StringBuilder errorMsg = new StringBuilder();
                    foreach (var err in rtn.ErrorMsg)
                    {
                        errorMsg.Append(err.Value);
                    }
                    TempData["executedResult"] = errorMsg.ToString();
                }
            }
            return View("IspItemSetUpdate", model.CheckListAnswerID);
        }
    }
}
